package com.woniuxy.config;

import com.woniuxy.util.RabbitmqConst;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @BelongsProject: pigTrotterStore
 * @BelongsPackage: com.woniuxy.config
 * @Author: Quan
 * @Date: 2025/3/21 10:13
 */
@Configuration
public class RabbitmqConfig {

	@Bean
	public Queue checkOrderStatusQueue() {
		return new Queue(RabbitmqConst.CHECK_ORDER_STATUS_QUEUE);
	}

	@Bean
	public CustomExchange delayExchange() {
		Map<String, Object> map = new HashMap<>();
		//指定交换机类型
		map.put("x-delayed-type","direct");
		/**
		 * 参数1： 指定交换机名字
		 * 参数2： 指定交换机消息类型（延迟消息）
		 * 参数3： 是否持久化
		 * 参数4： 是否自动删除
		 * 参数5： 其他参数（指定交换机类型）
		 */
		return new CustomExchange(RabbitmqConst.DELAY_EXCHANGE,"x-delayed-message",
				true,false,map);
	}

	@Bean
	public Binding checkOrderStatusToDelayExchange(Queue checkOrderStatusQueue,
												   CustomExchange delayExchange) {
		return BindingBuilder.bind(checkOrderStatusQueue).to(delayExchange).with("checkOrderStatus").noargs();
	}

	@Bean
	public Queue addOrderToMapperQueue() {
		return new Queue(RabbitmqConst.ADD_ORDER_TO_MAPPER_QUEUE);
	}

	@Bean
	public Queue addOrderItemToMapperQueue() {
		return new Queue(RabbitmqConst.ADD_ORDER_ITEM_TO_MAPPER_QUEUE);
	}

	@Bean
	public TopicExchange topicExchange() {
		return new TopicExchange(RabbitmqConst.TOPIC_EXCHANGE);
	}

	@Bean
	public Binding addOrderToMapperQueueToTopicExchange(Queue addOrderToMapperQueue,
												   TopicExchange topicExchange) {
		return BindingBuilder.bind(addOrderToMapperQueue).to(topicExchange).with("addOrderToMapper");
	}

	@Bean
	public Binding addOrderItemToMapperQueueToTopicExchange(Queue addOrderItemToMapperQueue,
															TopicExchange topicExchange) {
		return BindingBuilder.bind(addOrderItemToMapperQueue).to(topicExchange).with("addOrderItemToMapper");
	}
}
